<?php

class Pictet2_Controllers_View extends Pictet2_Controllers_Abstract {
	
	private $_metahandler;
	
	public function __construct($urlFolder, $params) {
		if($params[0]) {
			$view = Pictet2_Registry::getRegistry("view");
			if(!($engine = Pictet2_Settings::getSetting("ThumbnailEngine"))) {
				$engine = "Gd";
			}
			$class = Pictet2::getClassName("Thumbnailer_" . $engine);
			$thumbnailer = new $class();
			$metahandler = new Pictet2_Metahandlers_Xml();
			$dirIterator = new Pictet2_Iterators_Filters_Default(new Pictet2_Iterators_Directory(".", $thumbnailer), $thumbnailer);
			
			foreach($dirIterator as $file) {
				$files[] = $file->getFilename();
			}
			
			for($x = 0; $x <= sizeof($files)-1; ++$x) {
				if($files[$x] == $params[0]) {
					$index = $x;
					break;
				}
			}
			
			if($files[$index-1]) {
				if($metahandler->checkMetaModiefied($files[$index-1])) {
					$thumbnailer->makeThumb($files[$index-1]);
				}
				$view->assign("lastImage", $files[$index-1]);
			}
			
			if($files[$index+1]) {
				if($metahandler->checkMetaModiefied($files[$index+1])) {
					$thumbnailer->makeThumb($files[$index+1]);
				}
				$view->assign("nextImage", $files[$index+1]);
			}
			$this->_metahandler = $metahandler;
			$view->assign("galleryPage", ceil(($index+1)/12));
			
			$view->assign("image", $params[0]);
			$view->assign("title", " : " . $params[0]);
			$view->render("display");
		}
	}
	
	public function __destruct() {
		$this->_metahandler->saveMetadata();
	}
	
}